import { request } from './index'
import type { User } from '@/types'

export const userApi = {
  // 获取所有用户
  getAll(): Promise<User[]> {
    return request.get('/users')
  },

  // 根据ID获取用户
  getById(id: number): Promise<User> {
    return request.get(`/users/${id}`)
  },

  // 更新用户信息
  update(id: number, data: Partial<User>): Promise<User> {
    return request.put(`/users/${id}`, data)
  },

  // 删除用户
  delete(id: number): Promise<void> {
    return request.delete(`/users/${id}`)
  },

  // 修改密码
  changePassword(oldPassword: string, newPassword: string): Promise<void> {
    return request.post('/users/change-password', {
      oldPassword,
      newPassword
    })
  },

  // 根据用户名搜索用户
  searchByUsername(username: string): Promise<User[]> {
    return request.get(`/users/search?username=${username}`)
  },

  // 根据邮箱搜索用户
  searchByEmail(email: string): Promise<User[]> {
    return request.get(`/users/search?email=${email}`)
  },

  // 上传头像
  uploadAvatar(file: File): Promise<{ url: string }> {
    const formData = new FormData()
    formData.append('avatar', file)
    return request.post('/users/avatar', formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })
  }
}